﻿#include "camera_hk_qdialog.h"

#include "qpluginloader.h"
typedef CameraInterface* (*Fuction)();

CameraHkQDialog::CameraHkQDialog(QDialog *parent,QString path_in,QString section) : QDialog(parent)
{
    move(0,0);
    setFixedSize(1920,1000);
//    this->setModal(true);
    QGridLayout *qgridLayout = new QGridLayout(this);


    QVBoxLayout *qgridLayout_left = new QVBoxLayout();

    m_path_para = path_in;
    m_section   = section;


    QSize control_size(120,40);

    m_qlabel_exposure_time = new QLabel();
    m_qlabel_exposure_time->setText("曝光时长");
    m_qlabel_exposure_time->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_exposure_time,0,0);

    m_qspinbox_exposure_time = new QSpinBox();
    m_qspinbox_exposure_time->setFixedSize(control_size);
    m_qspinbox_exposure_time->setMaximum(100000);
    qgridLayout_left->addWidget(m_qspinbox_exposure_time,1,0);

    m_qlabel_gain = new QLabel();
    m_qlabel_gain->setText("增益");
    m_qlabel_gain->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_gain,2,0);

    m_qspinbox_gain = new QSpinBox();
    m_qspinbox_gain->setFixedSize(control_size);
    m_qspinbox_gain->setMinimum(1);
    m_qspinbox_gain->setMaximum(50);
    qgridLayout_left->addWidget(m_qspinbox_gain,3,0);

    m_qlabel_trigger_mode = new QLabel();
    m_qlabel_trigger_mode->setText("相机触发模式");
    m_qlabel_trigger_mode->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_trigger_mode,4,0);

    m_qcombox_trigger_mode = new QComboBox();
    m_qcombox_trigger_mode->setFixedSize(control_size);
    m_qcombox_trigger_mode->addItem("触发模式关闭");
    m_qcombox_trigger_mode->addItem("触发模式开启");
    qgridLayout_left->addWidget(m_qcombox_trigger_mode,5,0);

    m_qlabel_trigger_source = new QLabel();
    m_qlabel_trigger_source->setText("相机触发模式");
    m_qlabel_trigger_source->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_trigger_source,6,0);

    m_qcombox_trigger_source = new QComboBox();
    m_qcombox_trigger_source->setFixedSize(control_size);
    m_qcombox_trigger_source->addItem("line0");
    m_qcombox_trigger_source->addItem("line1");
    m_qcombox_trigger_source->addItem("line2");
    m_qcombox_trigger_source->addItem("line3");
    m_qcombox_trigger_source->addItem("Conuter0");
    m_qcombox_trigger_source->addItem("空");
    m_qcombox_trigger_source->addItem("空");
    m_qcombox_trigger_source->addItem("软触发");
    m_qcombox_trigger_source->addItem("变频器");
    qgridLayout_left->addWidget(m_qcombox_trigger_source,7,0);


    m_qpushbutton_soft_triger = new QPushButton();
    m_qpushbutton_soft_triger->setFixedSize(control_size);
    m_qpushbutton_soft_triger->setText("软件触发");
    qgridLayout_left->addWidget(m_qpushbutton_soft_triger,8,0);
    connect(m_qpushbutton_soft_triger, &QPushButton::clicked, this, &CameraHkQDialog::softTrigger);

    m_qpushbutton_save = new QPushButton();
    m_qpushbutton_save->setFixedSize(control_size);
    m_qpushbutton_save->setText("保存参数");
    qgridLayout_left->addWidget(m_qpushbutton_save,9,0);
    connect(m_qpushbutton_save, &QPushButton::clicked, this, &CameraHkQDialog::saveParameter);

    m_qpushbutton_hide = new QPushButton();
    m_qpushbutton_hide->setFixedSize(control_size);
    m_qpushbutton_hide->setText("退出");
    qgridLayout_left->addWidget(m_qpushbutton_hide,10,0);

//    connect(m_qpushbutton_hide,&QPushButton::clicked,this,&CameraHkQDialog::hide);
    qgridLayout->addLayout(qgridLayout_left,0,0);

    m_showqlable = new ShowQLabel(nullptr);
    m_showqlable->setFixedSize(1000,800);
    qgridLayout->addWidget(m_showqlable,0,1);
}

bool CameraHkQDialog::initCamera(QString name,CameraCallback pfunction, void *pcontext)
{
    QString m_exe_directory = QCoreApplication::applicationDirPath();

    QString filepath = QCoreApplication::applicationDirPath() + "/hk_camera.dll";


    QLibrary* mainFunDll = new QLibrary(filepath);
    bool state = mainFunDll->load();
    if (state)
    {

        Fuction f = (Fuction) mainFunDll->resolve("newInstance");
        if (f)
            m_camera_interface = f();


            if(m_camera_interface !=nullptr)
            {
                std::string string_name = name.toLocal8Bit().toStdString();
                bool state = m_camera_interface->camera_init( (char*)string_name.c_str(),pfunction, pcontext);
                if(state == false)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
    }
    else
    {
        return false;
    }
    loadParameter();
    return true;
}

void CameraHkQDialog::loadParameter()
{
    try
    {
        std::string path = m_path_para.toStdString();
        std::fstream f(path);
        if(!f.is_open())
        {
            QMessageBox::information(nullptr,"",m_section + "参数未设置");
            return ;
        }
        m_json_data = json::parse(f);
        f.close();

        if(!m_json_data.contains(m_section.toStdString()))
        {
            QMessageBox::critical(nullptr, m_section, "参数未设置");
            return ;
        }


        m_qspinbox_exposure_time->setValue(m_json_data[m_section.toStdString()]["m_qspinbox_exposure_time"]);

        m_qspinbox_gain->setValue(m_json_data[m_section.toStdString()]["m_qspinbox_gain"]);

        m_qcombox_trigger_mode->setCurrentIndex(m_json_data[m_section.toStdString()]["m_qcombox_trigger_mode"]);

        m_qcombox_trigger_source->setCurrentIndex(m_json_data[m_section.toStdString()]["m_qcombox_trigger_source"]);

        bool state;
        state = m_camera_interface->setExposureTime(m_qspinbox_exposure_time->value());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_section, "setExposureTime");
            return;
        }

        state = m_camera_interface->setGainValue(m_qspinbox_gain->value());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_section, "setGain");
            return;
        }

        state = m_camera_interface->setTriggerMode(m_qcombox_trigger_mode->currentIndex());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_section, "setTriggerMode");
            return;
        }

        state = m_camera_interface->setTriggerSource(m_qcombox_trigger_source->currentIndex());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_section, "trigger_source");
            return;
        }


    }
    catch(exception& error)
    {
        QMessageBox::critical(nullptr, m_section, QString::fromStdString(error.what()));
    }
}

void CameraHkQDialog::saveParameter()
{
    if(m_camera_interface == nullptr)
    {
        QMessageBox::critical(nullptr, m_section, "相机初始化失败");
        return;
    }

    bool state;
    state = m_camera_interface->setExposureTime(m_qspinbox_exposure_time->value());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "setExposureTime");
        return;
    }

    state = m_camera_interface->setGainValue(m_qspinbox_gain->value());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "setGain");
        return;
    }


    state = m_camera_interface->setTriggerMode(m_qcombox_trigger_mode->currentIndex());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "setTriggerMode");
        return;
    }

    state = m_camera_interface->setTriggerSource(m_qcombox_trigger_source->currentIndex());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "trigger_source");
        return;
    }

    m_json_data[m_section.toStdString()]["m_qspinbox_exposure_time"]    = m_qspinbox_exposure_time->value();
    m_json_data[m_section.toStdString()]["m_qspinbox_gain"]             = m_qspinbox_gain->value();
    m_json_data[m_section.toStdString()]["m_qcombox_trigger_mode"]             = m_qcombox_trigger_mode->currentIndex();
    m_json_data[m_section.toStdString()]["m_qcombox_trigger_source"]             = m_qcombox_trigger_source->currentIndex();

    std::string path = m_path_para.toStdString();
    std::ofstream o(path);
    o<< std::setw(4) << m_json_data << std::endl;

}

void CameraHkQDialog::showImage(Mat image,int camera_index,int index)
{
//    logger->info(m_section.toStdString() + "showCameraImage");

    if(image.empty())
    {
        QMessageBox::information(nullptr,"","相机读取图像出错");
        return;
    }

    double ratio_x =  double(m_showqlable->width()) /  double(image.cols) ;
    double ratio_y =  double(m_showqlable->height()) /  double(image.rows) ;
    double ratio_image = ratio_x < ratio_y ?  ratio_x : ratio_y;

    cv::resize(image, image, Size(image.cols * ratio_image , image.rows * ratio_image));

    QImage q_image = matToQImage(image);
    if (!q_image.isNull())
    {
        m_showqlable->loadtextimage(q_image,QString::number(camera_index) + "  "  + QString::number(index),QColor(0,255,0));
    }
}

void CameraHkQDialog::softTrigger()
{
    if(m_camera_interface == nullptr)
    {
        QMessageBox::critical(nullptr, m_section, "相机初始化失败");
        return;
    }
    bool state = m_camera_interface->softwareTrigger();
    if(state==false)
        QMessageBox::critical(nullptr, m_section, "softwareTrigger");
}
